package com.shouhuobao.samecity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.app.Animator;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.menu.WebViewActivity;
import com.collectplus.express.model.SameCityParcelBean;
import com.collectplus.express.model.SameCityPrePriceBean;
import com.collectplus.express.self.SelfRemarkActivity;
import com.collectplus.express.tools.SharedPref;
import com.collectplus.express.tools.TextWatcherExt;
import com.collectplus.express.wxapi.Constants;
import com.shouhuobao.bhi.loc.BaiduLocationService;
import com.shouhuobao.bhi.receiver.ReceiverCameraActivity;

import java.text.MessageFormat;
import java.util.HashMap;

import droid.frame.app.Callback;
import droid.frame.ui.utils.Utils;
import droid.frame.utils.http.BitmapUtils;
import droid.frame.utils.lang.Str;

import static com.collectplus.express.wxapi.Constants.SHB_RECEIVE_CODE;

/**
 * Created by joker on 2017/12/27.
 * 同城直送填写投寄信息界面
 */
public class SameCityFormActivity extends BaseActivity2 implements View.OnClickListener {

    private ImageView mBannerIV;//banner背景图
    private TextView mNearCourierTV;//附近的火箭侠展示数量
    private TextView mSendBuildNameTV;//寄件人建筑物名
    private TextView mSendAddressTV;//寄件人信息  详细地址
    private TextView mSendNameTV;//寄件人姓名
    private TextView mSendInfoTV;//寄件人的用户信息 手机号
    private TextView mReceiveBuildNameTV;//收件人建筑物
    private TextView mReceiveAddressTV;//收件人地址
    private TextView mReceiveNameTV;//收件人姓名
    private TextView mReceiveInfoTV;//收件人信息
    private TextView mGoodsType;// 物品类型
    private ImageView mGoodsImage;// 包裹照片
    private TextView mRemark;// 备注
    private TextView mRemarkState;// 备注状态
    private TextView mReceiverDown;// 下拉抽屉按钮
    private TextView mPreDistanceTimeTV;//1.5公司  预计1小时内送达
    private TextView mPriceTV;//同城直送的价位
    private Button mSubmit;// 提交
    private ViewGroup mBottomLayout;//底部布局
    private TextView mReceiveCodeTV;//收件码

    private String remark;
    private String preDiatanceTimeStr = "<font color=#404040>{0}    </font><font color=#F5A623>{1}</font>";
    private String nearCourierStr = "<font color=#ffffff>附近有</font><font color=#FFE200>{0}</font><font color=#ffffff>位火箭侠</font>";

    private SameCityParcelBean cacheParcelBean;
    private PopupWindow popwin = null;
    private String currentCity = "北京市";

    private String startPrice = "12.9元";
    private SameCityPrePriceBean prePriceBean = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 开始创建包裹
        cacheParcelBean = AppCache.getSameCityParcelBean();
        if (cacheParcelBean == null) {
            cacheParcelBean = new SameCityParcelBean();
            AppCache.setSameCityParcelBean(cacheParcelBean);
        }
        super.onCreate(savedInstanceState);
        loadDataFromCache();
        showLoadingDialog("定位中");
        Intent intent = new Intent();
        intent.setClass(getContext(), BaiduLocationService.class);
        startService(intent);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_same_city_form);
        super.findViewById();
        getAppTitle().setCommonTitle("同城直送");
        mBannerIV = (ImageView) findViewById(R.id.samecity_banner_iv);
        mNearCourierTV = (TextView) findViewById(R.id.near_courier_tv);
        mSendBuildNameTV = (TextView) findViewById(R.id.sender_buildname_tv);
        mSendAddressTV = (TextView) findViewById(R.id.sender_address_tv);
        mSendNameTV = (TextView) findViewById(R.id.send_name_tv);
        mSendInfoTV = (TextView) findViewById(R.id.send_info_tv);
        findViewById(R.id.select_send_ll).setOnClickListener(this);
        findViewById(R.id.select_receive_ll).setOnClickListener(this);
        mReceiveBuildNameTV = (TextView) findViewById(R.id.receive_buildname_tv);
        mReceiveAddressTV = (TextView) findViewById(R.id.receive_address_tv);
        mReceiveNameTV = (TextView) findViewById(R.id.receive_name_tv);
        mReceiveInfoTV = (TextView) findViewById(R.id.receive_info_tv);
        mGoodsImage = (ImageView) findViewById(R.id.send_goods_image);
        mGoodsImage.setOnClickListener(this);
        mGoodsType = (TextView) findViewById(R.id.send_goods_type);
        findViewById(R.id.receive_code_layout).setOnClickListener(this);
        mReceiveCodeTV = (TextView) findViewById(R.id.receive_code_tv);
        // 添加备注的布局
        findViewById(R.id.samecity_remark_layout).setOnClickListener(this);
        // 备注(选填、已添加)
        mRemark = (TextView) findViewById(R.id.receiver_remark);
        mRemarkState = (TextView) findViewById(R.id.receiver_remark_state);
        mReceiverDown = (TextView) findViewById(R.id.receiver_down);
        mPreDistanceTimeTV = (TextView) findViewById(R.id.pre_distance_time_tv);
        mPreDistanceTimeTV.setVisibility(View.INVISIBLE);
        mPriceTV = (TextView) findViewById(R.id.samecity_price_tv);
        findViewById(R.id.money_detail_iv).setOnClickListener(this);
        mSubmit = (Button) findViewById(R.id.subimt_order_bt);
        setButtonEnableStyle(mSubmit, false);
        mBottomLayout = (ViewGroup) findViewById(R.id.samecity_bottom_layout);
        mBottomLayout.setVisibility(View.GONE);
        findViewById(R.id.samecity_drawer_button_layout).setOnClickListener(this);
        findViewById(R.id.samecity_goods_layout).setOnClickListener(this);
        mSendBuildNameTV.addTextChangedListener(watcher);
        mSendAddressTV.addTextChangedListener(watcher);
        mSendNameTV.addTextChangedListener(watcher);
        mSendInfoTV.addTextChangedListener(watcher);
        mReceiveBuildNameTV.addTextChangedListener(watcher);
        mReceiveAddressTV.addTextChangedListener(watcher);
        mReceiveNameTV.addTextChangedListener(watcher);
        mReceiveInfoTV.addTextChangedListener(watcher);
        mGoodsType.addTextChangedListener(watcher);

        mPriceTV.setText(startPrice);
        mNearCourierTV.setText(Html.fromHtml(MessageFormat.format(nearCourierStr, 0)));
        mPreDistanceTimeTV.setText(Html.fromHtml(MessageFormat.format(preDiatanceTimeStr, "0公里", "预计0小时到达")));
    }

    private void loadDataFromCache() {
        // 从缓存中加载寄件人
        String sendBuildName = cacheParcelBean.getSendBuildName();
        String sendAddress = cacheParcelBean.getSendAddress();
        String sendAddressDesc = cacheParcelBean.getSendAddressDesc();
        String sendName = cacheParcelBean.getSendName();
        String sendPhone = cacheParcelBean.getSendPhone();
        // 注意先设置tag -- 因为setText以后会触发TextWatcher
        if (isEmpty(sendBuildName) && isEmpty(sendAddress)) {
            mSendBuildNameTV.setTag(null);
            mSendAddressTV.setTag(null);
            mSendBuildNameTV.setText("寄件人信息");
            mSendAddressTV.setText("");
        } else {
            if (isNotEmpty(sendBuildName)) {
                mSendBuildNameTV.setTag(sendBuildName);
                mSendBuildNameTV.setText(sendBuildName);
            }
            if (isNotEmpty(sendAddress)) {
                mSendAddressTV.setTag(sendAddress + sendAddressDesc);
                mSendAddressTV.setText("(" + sendAddress + sendAddressDesc + ")");
            }
        }
        if (isEmpty(sendName) && isEmpty(sendPhone)) {
            mSendNameTV.setTag(null);
            mSendInfoTV.setTag(null);
            mSendNameTV.setText("物品从哪里寄出");
            mSendInfoTV.setText("");
        } else {
            if (isNotEmpty(sendName)) {
                mSendNameTV.setTag(sendName);
                mSendNameTV.setText(sendName);
            }
            if (isNotEmpty(sendPhone)) {
                mSendInfoTV.setTag(sendPhone);
                mSendInfoTV.setText(sendPhone);
            }
        }
        // 从缓存中加载收件人
        String receiveBuildName = cacheParcelBean.getReceiveBuildName();
        String receiveAddress = cacheParcelBean.getReceiveAddress();
        String receiveAddressDesc = cacheParcelBean.getReceiveAddressDesc();
        String receiveName = cacheParcelBean.getReceiveName();
        String receivePhone = cacheParcelBean.getReceivePhone();
        // 注意先设置tag -- 因为setText以后会触发TextWatcher
        if (isEmpty(receiveBuildName) && isEmpty(receiveAddress)) {
            mReceiveBuildNameTV.setTag(null);
            mReceiveAddressTV.setTag(null);
            mReceiveBuildNameTV.setText("收件人信息");
            mReceiveAddressTV.setText("");
        } else {
            if (isNotEmpty(receiveBuildName)) {
                mReceiveBuildNameTV.setTag(receiveBuildName);
                mReceiveBuildNameTV.setText(receiveBuildName);
            }
            if (isNotEmpty(receiveAddress)) {
                mReceiveAddressTV.setTag(receiveAddress + receiveAddressDesc);
                mReceiveAddressTV.setText("(" + receiveAddress + receiveAddressDesc + ")");
            }
        }
        if (isEmpty(receiveName) && isEmpty(receivePhone)) {
            mReceiveNameTV.setTag(null);
            mReceiveInfoTV.setTag(null);
            mReceiveNameTV.setText("物品寄到哪里去");
            mReceiveInfoTV.setText("");
        } else {
            if (isNotEmpty(receiveName)) {
                mReceiveNameTV.setTag(receiveName);
                mReceiveNameTV.setText(receiveName);
            }
            if (isNotEmpty(receivePhone)) {
                mReceiveInfoTV.setTag(receivePhone);
                mReceiveInfoTV.setText(receivePhone);
            }
        }
        // 从缓存中加载图片
        String capturePath = cacheParcelBean.getImageUrl();
        if (isNotEmpty(capturePath)) {
            setGoodsImage(capturePath);
        }
        // 物品类型
        if (isNotEmpty(cacheParcelBean.getGoodsType())) {
            mGoodsType.setText(cacheParcelBean.getGoodsType() + " " + cacheParcelBean.getWeight() + "kg");
        }
        // 从缓存中加载备注
        remark = cacheParcelBean.getRemark();
        setRemarkText(remark);
        if (SharedPref.getReceiveCodeBoolean(SHB_RECEIVE_CODE)) {
            mReceiveCodeTV.setText("需要");
        } else {
            mReceiveCodeTV.setText("不需要");
        }
    }

    private TextWatcherExt watcher = new TextWatcherExt() {
        public void afterTextChanged(android.text.Editable s) {
            if (isNotEmpty(mSendBuildNameTV.getTag()) && isNotEmpty(mSendAddressTV.getTag()) && isNotEmpty(mSendNameTV.getTag()) && isNotEmpty(mSendInfoTV.getTag())
                    && isNotEmpty(mReceiveBuildNameTV.getTag()) && isNotEmpty(mReceiveAddressTV.getTag()) && isNotEmpty(mReceiveNameTV.getTag()) && isNotEmpty(mReceiveInfoTV.getTag())
                    && isNotEmpty(cacheParcelBean.getGoodsImage()) //
                    && isNotEmpty(mGoodsType.getText()) && isNotEmpty(cacheParcelBean.getWeight())) {
                mBottomLayout.setVisibility(View.VISIBLE);
                mSubmit.setOnClickListener(SameCityFormActivity.this);
                setButtonEnableStyle(mSubmit, true);
                showLoadingDialog(null);
                AppHttp.getInstance().queryPrePrice(cacheParcelBean.getSendLongitude(), cacheParcelBean.getSendLatitude(), currentCity, cacheParcelBean.getReceiveLongitude(), cacheParcelBean.getReceiveLatitude(), cacheParcelBean.getWeight());
            } else {
                mBottomLayout.setVisibility(View.GONE);
                mSubmit.setOnClickListener(null);
                setButtonEnableStyle(mSubmit, false);
            }
        }
    };

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //点击选中或取消收件码
            case R.id.receive_code_layout:
                DialogMgr.sameCityReceiverCodeSelector(this, SharedPref.getReceiveCodeBoolean(Constants.SHB_RECEIVE_CODE));
                break;
            //选择寄件人用户信息
            case R.id.select_send_ll:
                if (isNotEmpty(mSendBuildNameTV.getTag()) && isNotEmpty(mSendAddressTV.getTag()) && isNotEmpty(mSendNameTV.getTag()) && isNotEmpty(mSendInfoTV.getTag())) {
                    intent = new Intent(getContext(), SameCityInfoConfirmActivity.class);
                    intent.putExtra("type", 0);
                    intent.putExtra("loadType", 0);
                    intent.putExtra("isFrom", true);
                } else {
                    intent = new Intent(getContext(), SameCityAddressActivity.class);
                }
                startActivityForResult(intent, 101);
                break;
            //选择收件人信息
            case R.id.select_receive_ll:
                if (isNotEmpty(mReceiveBuildNameTV.getTag()) && isNotEmpty(mReceiveAddressTV.getTag()) && isNotEmpty(mReceiveNameTV.getTag()) && isNotEmpty(mReceiveInfoTV.getTag())) {
                    intent = new Intent(getContext(), SameCityInfoConfirmActivity.class);
                    intent.putExtra("type", 1);
                    intent.putExtra("loadType", 0);
                    intent.putExtra("isFrom", true);
                } else {
                    intent = new Intent(getContext(), SameCityReceiverAddressActivity.class);
                }
                startActivityForResult(intent, 102);
                break;
            // 添加备注
            case R.id.samecity_remark_layout:
                intent = new Intent();
                intent.setClass(getContext(), SelfRemarkActivity.class);
                intent.putExtra("remark", remark);
                startActivityForResult(intent, 104);
                break;
            // 选择物品类型
            case R.id.samecity_goods_layout:
                DialogMgr.showSameCityGoodsType(SameCityFormActivity.this, mGoodsType.getText().toString());
                break;
            // 最下面抽屉
            case R.id.samecity_drawer_button_layout:
                ViewGroup drawerLayout = (ViewGroup) findViewById(R.id.samecity_remark_layout);
                int height = findViewById(R.id.samecity_goods_layout).getHeight();
                if (drawerLayout.getVisibility() == View.VISIBLE) {
                    mReceiverDown.setText("填写更多信息");
                    Drawable drawableRight = getResources().getDrawable(R.drawable.drawer_down);
                    mReceiverDown.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableRight, null);
                    mReceiverDown.setCompoundDrawablePadding(Utils.dpToPx(5, getResources()));
                    Animator.scaleY(drawerLayout, null, height, 0);
                } else {
                    drawerLayout.setVisibility(View.VISIBLE);
                    mReceiverDown.setText("点击收起");
                    Drawable drawableRight = getResources().getDrawable(R.drawable.drawer_up);
                    mReceiverDown.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableRight, null);
                    mReceiverDown.setCompoundDrawablePadding(Utils.dpToPx(5, getResources()));
                    Animator.scaleY(drawerLayout, new Callback() {
                        @Override
                        public void execute(Object... result) {
                            ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview);
                            scrollView.scrollTo(0, scrollView.getBottom());
                        }
                    }, 0, height);
                }
                break;
            // 给包裹拍照
            case R.id.send_goods_image:
                intent = new Intent();
                intent.setClass(getContext(), ReceiverCameraActivity.class);
                intent.putExtra("type", "0");
                startActivityForResult(intent, 103);
                break;
            //费用明细查看
            case R.id.money_detail_iv:
                showChoosePop();
                break;
            //价格规则
            case R.id.price_order_tv:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("title", "计价规则");
                startActivity(intent);
                break;
            //提交订单
            case R.id.subimt_order_bt:
            case R.id.subimt_order_bt1://费用明细中的提交订单按钮
                if (cacheParcelBean != null) {
//                    if (Float.valueOf(cacheParcelBean.getWeight()) >= 50) {
//                        showToast("请不要超过50kg哦！");
//                        return;
//                    }
                    showLoadingDialog(null);
                    AppHttp.getInstance().orderImageUpdate(cacheParcelBean.getImageUrl());
                }
                if (popwin != null) {
                    popwin.dismiss();
                }
                break;
        }
    }

    private void setRemarkText(String remark) {
        if (isEmpty(remark)) {
            mRemark.setText("备注: ");
            mRemarkState.setText("选填");
            cacheParcelBean.setRemark("");
        } else {
            mRemarkState.setText("");
            mRemark.setText("备注: " + Str.filterEditText(remark));
            cacheParcelBean.setRemark(Str.filterEditText(remark));
        }
    }

    private void setGoodsImage(String capturePath) {
        Bitmap image = BitmapUtils.loadBitmapFromLocal(capturePath);
        if (image != null) {
            mGoodsImage.setImageBitmap(image);
        }
        // 拍照回调后隐藏文字，自动弹出选择物品类型
        findViewById(R.id.send_goods_above).setVisibility(View.GONE);
        // 抽屉按钮
        findViewById(R.id.samecity_drawer_button_layout).setVisibility(View.VISIBLE);
        // 物品类型
        findViewById(R.id.samecity_goods_layout).setVisibility(View.VISIBLE);
        ViewGroup receiveCodeLayout = (ViewGroup) findViewById(R.id.receive_code_layout);
        receiveCodeLayout.setVisibility(View.VISIBLE);
        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview);
        scrollView.scrollTo(0, scrollView.getBottom());

    }

    /**
     * 选择物品类型、回调
     *
     * @param goodsType
     */
    public void onGoodsTypeSelect(CharSequence goodsType, String goodsWeight) {
        cacheParcelBean.setGoodsType(goodsType + "");
        cacheParcelBean.setWeight(goodsWeight);
        mGoodsType.setText(goodsType + " " + goodsWeight + "kg");
        mGoodsType.setError(null);
        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview);
        scrollView.scrollTo(0, Utils.dpToPx(90, getResources()));
    }

    /**
     * 展示底部弹出的dialog
     */
    private void showChoosePop() {
        final View layout = LayoutInflater.from(this).inflate(R.layout.samecity_money_detail_pop, null);
        //
        popwin = DialogMgr.createPopupWindow(this, layout);
        popwin.showAtLocation(mBottomLayout, Gravity.RIGHT | Gravity.BOTTOM, 0, 0);
        //关闭
        layout.findViewById(R.id.money_detail_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popwin.dismiss();
            }
        });
        //基础配送费
        TextView basePriceTV = (TextView) layout.findViewById(R.id.base_price_tv);
        //距离
        TextView distanceTV = (TextView) layout.findViewById(R.id.distance_tv);
        //距离价格
        TextView distancePriceTV = (TextView) layout.findViewById(R.id.distance_price_tv);
        //重量
        TextView weightTV = (TextView) layout.findViewById(R.id.weight_tv);
        //重量价格
        TextView weightPriceTV = (TextView) layout.findViewById(R.id.weight_price_tv);
        //距离价钱描述
        TextView preDistanceTimeTV = (TextView) layout.findViewById(R.id.pre_distance_time_tv);
        //价钱
        TextView priceTV = (TextView) layout.findViewById(R.id.samecity_price_tv);
        layout.findViewById(R.id.price_order_tv).setOnClickListener(this);
        //提交订单
        Button submitBT = (Button) layout.findViewById(R.id.subimt_order_bt1);
        submitBT.setOnClickListener(this);
        if (isNotEmpty(mSendBuildNameTV.getTag()) && isNotEmpty(mSendAddressTV.getTag()) && isNotEmpty(mSendNameTV.getTag()) && isNotEmpty(mSendInfoTV.getTag())
                && isNotEmpty(mReceiveBuildNameTV.getTag()) && isNotEmpty(mReceiveAddressTV.getTag()) && isNotEmpty(mReceiveNameTV.getTag()) && isNotEmpty(mReceiveInfoTV.getTag())
                && isNotEmpty(cacheParcelBean.getGoodsImage()) //
                && isNotEmpty(mGoodsType.getText()) && isNotEmpty(cacheParcelBean.getWeight())) {
            setButtonEnableStyle(submitBT, true);
            submitBT.setOnClickListener(this);
        } else {
            setButtonEnableStyle(submitBT, false);
            submitBT.setOnClickListener(null);
        }
        if (prePriceBean != null) {
            basePriceTV.setText("￥" + startPrice.replace("元", ""));
            distanceTV.setText(prePriceBean.getDistanceDesc());
            distancePriceTV.setText("￥" + prePriceBean.getDistancePrice());
            weightTV.setText(prePriceBean.getWeight() + "kg");
            weightPriceTV.setText("￥" + prePriceBean.getWeightPrice());
            preDistanceTimeTV.setText(Html.fromHtml(MessageFormat.format(preDiatanceTimeStr, prePriceBean.getDistanceTotalDesc(), prePriceBean.getTimeDesc())));
            priceTV.setText(prePriceBean.getTotalPrice() + "元");
        } else {
            distanceTV.setText("0公里");
            distancePriceTV.setText("￥0");
            weightTV.setText("0kg");
            weightPriceTV.setText("￥0");
            priceTV.setText("12.9元");
            preDistanceTimeTV.setText(Html.fromHtml(MessageFormat.format(preDiatanceTimeStr, "0公里", "预计0小时到达")));
        }
    }

    /**
     * 更改收件码选中状态
     *
     * @param status
     */
    public void setReceiveCodeStatus(boolean status) {
        SharedPref.put(SHB_RECEIVE_CODE, status);
        if (status) {
            mReceiveCodeTV.setText("需要");
        } else {
            mReceiveCodeTV.setText("不需要");
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //定位
            case MsgID.location_baidu:
                cancelLoadingDialog();
                BDLocation mBdLocation = (BDLocation) msg.obj;
                if (mBdLocation == null) {
                    showToast("定位失败");
                } else {
                    showLoadingDialog("查询中");
                    if (isNotEmpty(mBdLocation.getCity())) {
                        currentCity = mBdLocation.getCity();
                        AppHttp.getInstance().initCityParcelInfo(mBdLocation.getLongitude(), mBdLocation.getLatitude(), mBdLocation.getCity());
                    } else {
                        AppHttp.getInstance().initCityParcelInfo(mBdLocation.getLongitude(), mBdLocation.getLatitude(), "北京市");
                    }
                }
                return false;
            //定位初始化价格
            case MsgID.samecity_init_parcel_info:
                cancelLoadingDialog();
                AppResult<Integer> result = JSON.parseMap(msg.obj, Integer.class);
                if (result.isSuccess()) {
                    final HashMap<String, Integer> map = result.getResultMap();
                    if (map != null && map.size() > 0) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (map.get("courierCnt") != null) {
                                    mNearCourierTV.setText(Html.fromHtml(MessageFormat.format(nearCourierStr, map.get("courierCnt"))));
                                }
                                if (map.get("startPrice") != null) {
                                    startPrice = map.get("startPrice") + "元";
                                }
                                mPriceTV.setText(startPrice);
                            }
                        });
                    }
                } else {
                    showToast(result.getMessage());
                }
                return true;
            //预支付价格
            case MsgID.samecity_pre_price:
                cancelLoadingDialog();
                AppResult<SameCityPrePriceBean> appResult = JSON.parser(msg.obj, SameCityPrePriceBean.class);
                if (appResult.isSuccess()) {
                    prePriceBean = appResult.getResult();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (prePriceBean != null) {
                                mPreDistanceTimeTV.setVisibility(View.VISIBLE);
                                mPreDistanceTimeTV.setText(Html.fromHtml(MessageFormat.format(preDiatanceTimeStr, prePriceBean.getDistanceTotalDesc(), prePriceBean.getTimeDesc())));
                                startPrice = prePriceBean.getStartPrice() + "元";
                                mPriceTV.setText(prePriceBean.getTotalPrice() + "元");
                            }
                        }
                    });
                } else {
                    showToast(appResult.getMessage());
                }
                return true;
            //上传图片返回
            case MsgID.order_upload_image:
                cancelLoadingDialog();
                AppResult<String> result1 = JSON.parser(msg.obj, String.class);
                if (result1.isSuccess()) {
                    showLoadingDialog("提交订单");
                    cacheParcelBean.setParcelImgUrl(result1.getResult());
                    AppHttp.getInstance().sameCityOrderSubmit();
                } else {
                    showToast(result1.getMessage());
                }
                return true;
            //提交订单返回
            case MsgID.samecity_order_submit:
                cancelLoadingDialog();
                AppResult<String> orderSubmitResult = JSON.parseMap(msg.obj, String.class);
                if (orderSubmitResult.isSuccess()) {
                    HashMap<String, String> map = orderSubmitResult.getResultMap();
                    if (map != null && map.size() > 0) {
                        String orderId = map.get("orderId");
                        String parcelId = map.get("parcelId");
                        if (isNotEmpty(orderId)) {
                            Intent intent = new Intent(getContext(), SameCityOrderActivity.class);
                            intent.putExtra("orderId", orderId);
                            startActivity(intent);
                            AppCache.setSameCityParcelBean(null);
                            finish();
                        }
                    }
                } else if (orderSubmitResult.getStatus() == 2) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DialogMgr.noCourierCnt(SameCityFormActivity.this);
                        }
                    });
                } else {
                    showToast(orderSubmitResult.getMessage());
                }

                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 选择寄件人 - 回调
        if (requestCode == 101 && resultCode == RESULT_OK) {
            if (isNotEmpty(cacheParcelBean.getSendBuildName())) {
                mSendBuildNameTV.setTag(cacheParcelBean.getSendBuildName());
                mSendBuildNameTV.setText(cacheParcelBean.getSendBuildName());
            } else {
                mSendBuildNameTV.setTag(null);
            }
            if (isNotEmpty(cacheParcelBean.getSendAddress())) {
                mSendAddressTV.setTag(cacheParcelBean.getSendAddress() + cacheParcelBean.getSendAddressDesc());
                mSendAddressTV.setText("(" + cacheParcelBean.getSendAddress() + cacheParcelBean.getSendAddressDesc() + ")");
            } else {
                mSendAddressTV.setTag(null);
            }
            if (isNotEmpty(cacheParcelBean.getSendName())) {
                mSendNameTV.setTag(cacheParcelBean.getSendName());
                mSendNameTV.setText(cacheParcelBean.getSendName());
            } else {
                mSendNameTV.setTag(null);
            }
            if (isNotEmpty(cacheParcelBean.getSendPhone())) {
                mSendInfoTV.setTag(cacheParcelBean.getSendPhone());
                mSendInfoTV.setText(cacheParcelBean.getSendPhone());
            } else {
                mSendInfoTV.setTag(null);
            }
        }
        //收件人选择地址回调
        else if (requestCode == 102 && resultCode == RESULT_OK) {
            if (isNotEmpty(cacheParcelBean.getReceiveBuildName())) {
                mReceiveBuildNameTV.setTag(cacheParcelBean.getReceiveBuildName());
                mReceiveBuildNameTV.setText(cacheParcelBean.getReceiveBuildName());
            } else {
                mReceiveBuildNameTV.setTag(null);
            }
            if (isNotEmpty(cacheParcelBean.getReceiveAddress())) {
                mReceiveAddressTV.setTag(cacheParcelBean.getReceiveAddress() + cacheParcelBean.getReceiveAddressDesc());
                mReceiveAddressTV.setText("(" + cacheParcelBean.getReceiveAddress() + cacheParcelBean.getReceiveAddressDesc() + ")");
            } else {
                mReceiveAddressTV.setTag(null);
            }
            if (isNotEmpty(cacheParcelBean.getReceiveName())) {
                mReceiveNameTV.setTag(cacheParcelBean.getReceiveName());
                mReceiveNameTV.setText(cacheParcelBean.getReceiveName());
            } else {
                mReceiveNameTV.setTag(null);
            }
            if (isNotEmpty(cacheParcelBean.getReceivePhone())) {
                mReceiveInfoTV.setTag(cacheParcelBean.getReceivePhone());
                mReceiveInfoTV.setText(cacheParcelBean.getReceivePhone());
            } else {
                mReceiveInfoTV.setTag(null);
            }
        }
        // 拍照 -- 回调
        else if (requestCode == 103 && resultCode == RESULT_OK) {
            String capturePath = data.getStringExtra("file");
            // cache
            cacheParcelBean.setGoodsImage(capturePath);
            setGoodsImage(capturePath);
            DialogMgr.showSameCityGoodsType(SameCityFormActivity.this, mGoodsType.getText().toString());
            //
            mGoodsImage.setTag("");// set not null
            watcher.afterTextChanged(null);
        } else if (requestCode == 104 && resultCode == RESULT_OK) {
            remark = data.getStringExtra("remark");
            setRemarkText(remark);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popwin != null) {
            popwin.dismiss();
        }
    }
}
